$(function() {
	var id = getvl("id");
	if(id != '') {
		orderDetail(id);
	}

});

function orderDetail(id) {
	$("#myAddress").html("");
	$('#shopList').html("");
	$('#paystatus').html("");
	$.ajax({
		url: urlcore + "/api/order/selectOne1?id=" + id,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				var da = data.data;
				if(da.status == 0) {
					var paystatus1 =
						'<ul class="zhuangtai_tiaomu clearfix">' +
						'<li>' +
						'<img src="images/zhuangtai01.png" />' +
						'<h6>提交订单</h6>' +
						'<p>' + da.gmtDatetime + '</p>' +
						'</li>' +
						'<li>' +
						'<img src="images/zhuangtai2.png" />' +
						'<h6>付款</h6>' +
						'</li>' +
						'<li>' +
						'<img src="images/zhuangtai2.png" />' +
						'<h6>发货</h6>' +
						'</li>' +
						'<li>' +
						'<img src="images/zhuangtai3.png" />' +
						'<h6>确认收货</h6>' +
						'</li>' +
						'</ul>' +
						'<div class="detail_zhuangtai_xinxi">' +
						'<h6>当前订单状态：<em>等待付款</em></h6>' +
						'<p>请及时付款，该订单将在' +
						'<div id="demo02">' +
						'<em class="hour"></em>:' +
						'<em class="minute">-</em>:' +
						'<em class="second">-</em></div>' +
						'后自动关闭</p>' +
						'<div>' +
						'<span onclick="cancelDinDan(' + da.id + ');" style="cursor:pointer;">取消订单</span>' +
						'<a href="#" class="mspay">立即付款</a>' +
						'</div>' +
						'</div>';

					$('#paystatus').html(paystatus1);
					//倒计时
					$(function() {

						//Date curDate = new Date(da.gmtDatetime);
						//alert(curDate);
						//var preDate = new Date(curDate.getTime() - 24 * 60 * 60 * 1000); //前一天
						var nextDate = new Date(new Date(da.gmtDatetime).getTime() + 24 * 60 * 60 * 1000); //后一天
						countDown(nextDate, null, "#demo02 .hour", "#demo02 .minute", "#demo02 .second", da.id);

						$('.mspay').click(function() {
							var money = $(this).closest(".detail_zhuangtai").parent().find('input[class*=paymoney]').val();
							if(!layer.confirm("是否去支付？")) {
								return;
							}
							if(money != '') {
								//把支付金额存入
								sessionStorage.setItem('money', money);
								window.location.href = "choice_pay.html";
							}
						})

					});
				} else if(da.status == 1 && da.tradeStatus == 0) {
					var paystatus2 =
						'<ul class="zhuangtai_tiaomu clearfix">' +
						'<li>' +
						'<img src="images/zhuangtai01.png" />' +
						'<h6>提交订单</h6>' +
						'<p>' + da.gmtDatetime + '</p>' +
						'</li>' +
						'<li>' +
						'<img src="images/zhuangtai02.png" />' +
						'<h6>付款</h6>' +
						'</li>' +
						'<li>' +
						'<img src="images/zhuangtai2.png" />' +
						'<h6>发货</h6>' +
						'</li>' +
						'<li>' +
						'<img src="images/zhuangtai3.png" />' +
						'<h6>确认收货</h6>' +
						'</li>' +
						'</ul>' +
						'<div class="detail_zhuangtai_xinxi">' +
						'<h6>当前订单状态：<em>等待发货</em></h6>' +
						'</div>';
					$('#paystatus').html(paystatus2);

				} else if(da.status == 1 && da.tradeStatus == 1) {
					var paystatus3 =
						'<ul class="zhuangtai_tiaomu clearfix">' +
						'<li>' +
						'<img src="images/zhuangtai01.png" />' +
						'<h6>提交订单</h6>' +
						'<p>' + da.gmtDatetime + '</p>' +
						'</li>' +
						'<li>' +
						'<img src="images/zhuangtai02.png" />' +
						'<h6>付款</h6>' +
						'</li>' +
						'<li>' +
						'<img src="images/zhuangtai02.png" />' +
						'<h6>发货</h6>' +
						'</li>' +
						'<li>' +
						'<img src="images/zhuangtai3.png" />' +
						'<h6>确认收货</h6>' +
						'</li>' +
						'</ul>' +
						'<div class="detail_zhuangtai_xinxi">' +
						'<h6>当前订单状态：<em>已发货</em></h6>' +
						'</div>';
					$('#paystatus').html(paystatus3);

				} else if(da.status == 2 && da.tradeStatus == 2) {
					var paystatus4 =
						'<ul class="zhuangtai_tiaomu clearfix">' +
						'<li>' +
						'<img src="images/zhuangtai01.png" />' +
						'<h6>提交订单</h6>' +
						'<p>' + da.gmtDatetime + '</p>' +
						'</li>' +
						'<li>' +
						'<img src="images/zhuangtai02.png" />' +
						'<h6>付款</h6>' +
						'</li>' +
						'<li>' +
						'<img src="images/zhuangtai02.png" />' +
						'<h6>发货</h6>' +
						'</li>' +
						'<li>' +
						'<img src="images/zhuangtai03.png" />' +
						'<h6>确认收货</h6>' +
						'</li>' +
						'</ul>' +
						'<div class="detail_zhuangtai_xinxi">' +
						'<h6>当前订单状态：<em>确认收货</em></h6>' +
						'</div>';
					$('#paystatus').html(paystatus4);

				}

				var remark = da.remark == null ? '暂无' : da.remark;
				var addressList =
					'<div class="dengtai_xinxi_mai">' +
					'<div class="dengtai_xinxi_mai_title">' +
					'<span>订单详情</span>' +
					'</div>' +
					'<ul>' +
					'<li>买家信息</li>' +
					'<li>订&nbsp;&nbsp;单&nbsp;号：' + da.orderNumber + '</li>' +
					'<li>下单时间：' + da.gmtDatetime + '</li>' +
					'<li>买家留言：' + remark + '</li>' +
					'<li>收&nbsp;&nbsp;货&nbsp;人：' + da.user.userName + '</li>' +
					'<li>收货地址：' + da.address.province + '' + da.address.city + '' + da.address.county + '' + da.address.address + '</li>' +
					'<li>联系电话：' + da.address.userPhone + '</li>' +
					'</ul>' +
					'</div>';
				$("#myAddress").append(addressList);
				var paymoney = 0; //支付总额
				var saleNums = 0; //总件数
				var goodsList = '';
				$.each(da.goodsList, function(i, n) {
					var goodsStockList = '';
					var saleNum = 0; //每个商品件数
					var money = 0; //单件商品金额
					$.each(n.myStockList, function(i, s) {
						saleNum += s.saleNum; //计算总个数

						goodsStockList +=
							'<tr>' +
							'<td>' +
							'<span>' + s.size.sizeName + '</span>' +
							'</td>' +
							'<td>' + s.colour.colourName + '</td>' +
							'<td>' + s.saleNum + '件</td>' +
							'<td class="saleNum" hidden="hidden"><input type="hidden" class="saleNum1" value="' + s.saleNum + '"/></td>' +
							'</tr>';

					});
					saleNums += saleNum;

					//var coverUrl = n.coverUrl == "" ? "" : n.coverUrl;
					var coverUrl =isHasImg(n.coverUrl);
					var goodsTitle = n.goodsTitle == "" ? "" : n.goodsTitle;
					var tradePrice = n.tradePrice == "" ? "" : n.tradePrice;
					var packPrice = n.packPrice == "" ? "" : n.packPrice;
					//判断是否是优惠价
					var packRate = 0;
					if(n.goodsCar!=null){
						if(n.goodsCar.goodsType == 1) {
							if(saleNum >= n.packNumber) {
								packRate = packPrice;
							} else {
								packRate = tradePrice;
							}
						 }else {
						       packRate = n.goodsCar.packRate;
					     } 
					}else{
							   packRate = tradePrice;
					}

					//每个商品的金额
					money += packRate * saleNum;
					paymoney += money; //计算总支付金额
					goodsList += '<div class="clearfix detail_goods">' +
						'<dl class="fl clearfix">' +
						'<dt>' +
						'<a href="2.2.0goods_detail.html?id=' + n.id + '"><img src="' + coverUrl + '" /></a>' +
						'</dt>' +
						'<dd>' +
						'<h5><a href="2.2.0goods_detail.html?id=' + n.id + '">' + goodsTitle + '</a></h5>' +
						'<h6>&yen;' + packRate + '</h6>' +
						'</dd>' +
						'</dl>' +
						'<div class="fl detail_goods_table">' +
						'<table>' +
						goodsStockList +
						'</table>' +
						'</div>' +
						'<div class="fr detail_goods_price">&yen;' + money + '</div>' +
						'</div>';
				});
				var freightMoney=da.freightMoney==null?0:da.freightMoney;
				var goldMoney=da.goldMoney==null?0:da.goldMoney;
				var shop =
					'<div class="detail_xinxi_goods">' +
					'<div style="height: 1px; background: #F5F5F5;"></div>' +
					'<div class="putong_list">' +
					'<div class="putong_list_title">' +
					'<span>2016-05-30</span>' +
					'<span>订单号 : ' + da.orderNumber + '</span>' +
					'<a href="10.1.0dianpu_detail.html?id=' + da.shop.id + '"><em>' + da.shop.storeName + '</em></a>' +
					'</div>' +
					goodsList +
					'</div>' +
					'<ul class="detail_feiyong">' +
					'<li>商品总计<em>' + saleNums + '</em>件商品总价<span>' + da.orderMoney + '</span>元</li>' +
					'<li>总运费<span>'+freightMoney+'</span>元</li>' +
					'<li>金币抵扣<span>'+goldMoney+'</span>元</li>' +
					'<li>总计(含运费)<span><input type="hidden" class="paymoney" value="' + da.payMoney + '"/>' + da.payMoney + '</span>元</li>' +
					'</ul>' +
					'</div>';
				$('#shopList').append(shop);
			}
		},
		error: function() {
			layer.alert("error");
		}
	});
}

function pay(paytype, money) {
	if(paytype == 3) {
		$.ajax({
			url: urlcore + "/api/pay/pc/wxpay?money=" + money,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				if(data.success == true) {
					var da = data.data;
					toPay(da.code_url);
				}
			},
			error: function() {
				layer.alert("error");
			}
		});
	} else {
		location.href = urlcore + "/api/pay/pc/alipay?money=" + money;
		/*$.ajax({
		url: urlcore + "/api/pay/pc/alipay?money=" + money,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				var da = data.data;

			}
		},
		error: function() {
			alert("error");
		}
	});*/
	}

}

function toPay(url) {
	$("#QRCode").html('');

	$.ajax({
		url: urlcore + "/api/QRCodeEncoderHandler/encoderQRCode?content=" + url,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {

				var da = data.data;
				var img = '<img src="qrcode/' + da.img + '" />'
				$("#QRCode").html(img);

				//二维码显示与取消
				$(function() {
					$(".public_simi_bg").fadeIn();

					$(".public_shuliang h5 span,.public_shuliang_footer a").on('click', function() {
						$(".public_simi_bg").fadeOut();
					})
				})

			}
		},
		error: function() {
			layer.alert("error");
		}
	});

}

function countDown(time, day_elem, hour_elem, minute_elem, second_elem, id) {

	//if(typeof end_time == "string")
	var end_time = new Date(time).getTime(), //月份是实际月份-1
		//current_time = new Date().getTime(),
		sys_second = (end_time - new Date().getTime()) / 1000;
	var timer = setInterval(function() {
		if(sys_second > 0) {
			sys_second -= 1;
			var day = Math.floor((sys_second / 3600) / 24);
			var hour = Math.floor((sys_second / 3600) % 24);
			var minute = Math.floor((sys_second / 60) % 60);
			var second = Math.floor(sys_second % 60);
			day_elem && $(day_elem).text(day); //计算天
			$(hour_elem).text(hour < 10 ? "0" + hour : hour); //计算小时
			$(minute_elem).text(minute < 10 ? "0" + minute : minute); //计算分
			$(second_elem).text(second < 10 ? "0" + second : second); // 计算秒
		} else {
			clearInterval(timer);
			//到时间自动关闭
			cancelDinDan1(id);
		}
	}, 1000);
}

function cancelDinDan1(id) {

	$.ajax({
		url: urlcore + '/api/order/update1',
		type: "post",
		contentType: "application/json;charset=utf-8",
		data: JSON.stringify({
			"id": id,
			"status": 7
		}),
		dataType: "json",
		success: function(data) {
			if(data.success == true) {

				//跳转列表页
				window.location.href = "20.0.0wode_dingdan.html";
			} else if(data.code == 'OVERTIME') {
				var thisUrl = window.location.href;
				if(thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href = "1.0.0denglu.html";
				}
			} else {
				layer.alert(data.msg);
			}

		},
		error: function() {
			layer.alert("error");
		}

	});
}

function cancelDinDan(id) {
	if(!layer.confirm("确认取消吗?")) {
		return;
	}
	$.ajax({
		url: urlcore + '/api/order/update1',
		type: "post",
		contentType: "application/json;charset=utf-8",
		data: JSON.stringify({
			"id": id,
			"status": 7
		}),
		dataType: "json",
		success: function(data) {
			if(data.success == true) {
				layer.alert("取消成功");
				//跳转列表页
				window.location.href = "20.0.0wode_dingdan.html";
			} else if(data.code == 'OVERTIME') {
				var thisUrl = window.location.href;
				if(thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href = "1.0.0denglu.html";
				}
			} else {
				layer.alert(data.msg);
			}

		},
		error: function() {
			layer.alert("error");
		}

	});
}